package com.heatingcharge.controller.sys;


import com.heatingcharge.common.util.R;
import com.heatingcharge.dto.DeptLevelDTO;
import com.heatingcharge.param.DeptParam;
import com.heatingcharge.service.sys.ISysDeptService;
import com.heatingcharge.service.SysTreeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author devin.jiang
 * @since 2019-06-26
 */
@RestController
@RequestMapping("/sys-dept")
public class SysDeptController {

    @Autowired
    ISysDeptService sysDeptService;
    @Autowired
    SysTreeService sysTreeService;

    @ApiOperation(value = "获取部门信息",notes = "获取部门信息")
    @GetMapping
//    @PreAuthorize("hasAuthority('sys:dept:view')")
    public R getDept(){
        List<DeptLevelDTO> deptTree = sysTreeService.getDept();
        return R.ok(deptTree);
    }

    @ApiOperation(value = "新增部门",notes = "新增部门")
    @PostMapping
    public R saveDept(@RequestBody DeptParam deptParam){
        if (sysDeptService.save(deptParam)>0){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "更新部门",notes = "更新部门")
    @PutMapping
    public R updateDept(@RequestBody DeptParam deptParam){
        if (sysDeptService.update(deptParam)>0){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "删除部门",notes = "根据主键ID删除部门")
    @DeleteMapping("/{id}")
    public R delDept(@PathVariable("id") Integer id){
        if (sysDeptService.removeById(id)){
            return R.ok();
        }
        return R.error();
    }
}

